package org.tgcloud.group.web.controller.manager;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.custom.Permissions;
import org.tgcloud.group.core.session.Session;
import org.tgcloud.group.service.manager.MediaManageService;

/**
 * @Author: zhy
 * @Created: 2025-02-11-下午2:31
 * @Description:
 */

@Api(tags = "媒体资源管理相关接口")
@RestController
@RequestMapping("/manager/pc/user")
@EnableAutoConfiguration
public class MediaManageController extends ManageAPPBaseController {

    @Autowired
    private MediaManageService mediaManageService;


    @Permissions("mediaType:view")
    @ApiOperation(value = "媒体类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/getMediaTypeList", method = RequestMethod.POST)
    public APPResultMessage getMediaTypeList(@ModelAttribute("session") Session session,
                                         @RequestParam(required = false) Integer pageNum,
                                         @RequestParam(required = false) Integer pageSize,
                                         @RequestParam(required = false) String keyword){
        return mediaManageService.getMediaTypeList(pageNum, pageSize, keyword);
    }


    @Permissions("mediaType:add")
    @ApiOperation(value = "新增媒体类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/addMediaType", method = RequestMethod.POST)
    public APPResultMessage addMediaType(@ModelAttribute("session") Session session,
                                             @RequestParam String name,
                                             @RequestParam String iconPath,
                                         @RequestParam(required = false) Integer sort){
        return mediaManageService.addMediaType(name, iconPath, sort);
    }


    @Permissions("mediaType:update")
    @ApiOperation(value = "修改媒体类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/updateMediaType", method = RequestMethod.POST)
    public APPResultMessage updateMediaType(@ModelAttribute("session") Session session,
                                         @RequestParam int id,
                                         @RequestParam String name,
                                         @RequestParam String iconPath,
                                            @RequestParam(required = false) Integer sort){
        return mediaManageService.updateMediaType(id, name, iconPath, sort);
    }


    @Permissions("mediaType:del")
    @ApiOperation(value = "删除媒体类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/delMediaType", method = RequestMethod.POST)
    public APPResultMessage delMediaType(@ModelAttribute("session") Session session,
                                         @RequestParam int id){
        return mediaManageService.delMediaType(id);
    }


    @Permissions("media:view")
    @ApiOperation(value = "媒体资源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/getMediaList", method = RequestMethod.POST)
    public APPResultMessage getMediaList(@ModelAttribute("session") Session session,
                                        @RequestParam(required = false) Integer pageNum,
                                        @RequestParam(required = false) Integer pageSize,
                                        @RequestParam(required = false) String keyword,
                                        @RequestParam(required = false) Integer mediaType){
        return mediaManageService.getMediaList(pageNum, pageSize, keyword, mediaType);
    }


    @Permissions("media:add")
    @ApiOperation(value = "新增媒体资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/addMedia", method = RequestMethod.POST)
    public APPResultMessage addMedia(@ModelAttribute("session") Session session,
                                         @RequestParam String title,
                                         @RequestParam Integer mediaType,
                                         @RequestParam String address,
                                         @RequestParam String otherMsg){
        return mediaManageService.addMedia(title, mediaType, address, otherMsg);
    }


    @Permissions("media:update")
    @ApiOperation(value = "修改媒体资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/updateMedia", method = RequestMethod.POST)
    public APPResultMessage updateMedia(@ModelAttribute("session") Session session,
                                     @RequestParam Integer id,
                                     @RequestParam String title,
                                     @RequestParam Integer mediaType,
                                     @RequestParam String address,
                                     @RequestParam String otherMsg){
        return mediaManageService.updateMedia(id, title, mediaType, address, otherMsg);
    }


    @Permissions("media:del")
    @ApiOperation(value = "删除媒体资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/delMedia", method = RequestMethod.POST)
    public APPResultMessage delMedia(@ModelAttribute("session") Session session,
                                     @RequestParam int id){
        return mediaManageService.delMedia(id);
    }


    @Permissions("media:add")
    @ApiOperation(value = "导入媒体资源excel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/importMediaExcel", method = RequestMethod.POST)
    public APPResultMessage doImportMediaExcel(@RequestPart("file") MultipartFile file) throws Exception{
        return mediaManageService.doImportMediaExcel(file);
    }


    @ApiOperation(value = "刷新缓存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/refreshRedis", method = RequestMethod.POST)
    public void doRefreshRedis(){
        mediaManageService.doRefreshRedis();
    }



    @Permissions("media:del")
    @ApiOperation(value = "批量删除媒体资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string")
    })
    @ResponseBody
    @RequestMapping(value = "/delBatchMedia", method = RequestMethod.POST)
    public APPResultMessage delBatchMedia(@ModelAttribute("session") Session session,
                                          @RequestParam Integer mediaType,
                                          @RequestParam String channelName){
        return mediaManageService.delBatchMedia(mediaType, channelName);
    }


}
